import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    num:1
  },
  getters: {
  },
  mutations: {
    //方法名(state){//操作state的数据}
    increment(state){
      state.num++
      //将state.num数据保存到localStorage中
      window.localStorage.setItem('num_key',JSON.stringify(state.num))
    },
    setNum(state,n){
      state.num=n
    }
  },
  actions: {
    loadNum(cxt){
      //从localStorage中获取num_key的数据
      let n=~~localStorage.getItem('num_key')
      //调用mutations中setNum来完成对仓库中state的num数据的更新
      cxt.commit('setNum',n)
    }
  },
  modules: {
  }
})
